package com.qk.controller;

import com.github.pagehelper.Page;
import com.qk.Response.PageResult;
import com.qk.Response.Result;
import com.qk.dto.RolePageDTO;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;
    /**
     * 查询角色列表(分页)
     * @return
     */
    @GetMapping
    public Result getRoles(RolePageDTO rolePageDTO){
        log.info("查询角色列表：{}",rolePageDTO);
        PageResult<Role> pageResult =roleService.getRoles(rolePageDTO);
        return Result.success(pageResult);
    }
    /*
    添加角色
     */
    @PostMapping
    public Result addRole(@RequestBody Role role){
        log.info("添加角色：{}",role);
        roleService.addRole(role);
        return Result.success();
    }
    /*
    删除角色
     */
    @DeleteMapping("/{id}")
    public Result deleteRoleById(@PathVariable Integer id){
        log.info("删除角色：{}",id);
        roleService.deleteRoleById(id);
        return Result.success();
    }
    /*
    更新角色
     */
    /*
    根据id查询角色
     */
    @GetMapping("/{id}")
    public Result selectRoleById(@PathVariable Integer id){
        log.info("查询角色：{}",id);
        Role role=roleService.selectRoleById(id);
        return Result.success(role);
    }
    /*
    更新角色
     */
    @PutMapping
    public Result updateRole(@RequestBody Role role){
        log.info("更新角色：{}",role);
        roleService.updateRole(role);
        return Result.success();
    }
    /*
    查询全部角色
     */
    @GetMapping("/list")
    public Result selectAllRole(){
        log.info("查询全部角色");
        List<Role> roles=roleService.selectAllRole();
        return Result.success(roles);
    }

}
